package com.xiaonei.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.util.DBConnection;
import com.xiaonei.model.Post;

import java.util.ArrayList;

//import com.sun.tools.javac.util.List;

/**
 *主贴操作�? *主贴操作类的具体实现，包括查找主贴，显示主贴主要信息�? *@author  HJJ
 */

public  class PostDAOImpl implements IPostDAO{
	/**
     *查找主贴方法
     *根据帖子ID查找该帖子内�?     *@param  post_num为帖子ID号；
     *@return content�?     */	
   public String searchPost(int post_num){
	   String content=new String("");//贴吧内容�?	   
	   Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
	   ResultSet rs=null;//结果�?		
	   PreparedStatement pstmt=null;
		String sql="select post_num,content from post where post_num='"+post_num+"'";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				content=rs.getString(content);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return content;
   }
   
   /**
  	 *打印主帖主要信息方法
  	 *根据帖子IdList显示这些帖子的简要信�?  	 *@param  ArrayList<String>为帖子id号的集合�?  	 *@return 返回ArrayList()型；
  	 */
   public ArrayList<Post> postList(ArrayList<String> idlist){
	   ArrayList<Post> postList=new ArrayList<Post>();
	   Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
	   ResultSet rs=null;//结果�?		
	   PreparedStatement pstmt=null;
		try{
		for(int i=0;i<idlist.size();i++){
		String sql="select * from post where post_num='"+idlist.get(i)+"'";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Post e=new Post();
				e.setPost_num(rs.getInt("post_num"));
				e.setAccount(rs.getString("account"));
				e.setContent(rs.getString("content"));
				e.setImageUrl(rs.getString("imageurl"));
				e.setTheme(rs.getString("theme"));
				e.setTopic_num(rs.getInt("topic_num"));
				e.setTime(rs.getString("time"));
				postList.add(e);
			}	
		}
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return postList;   
   }
   
   /**
 	 *查找帖子id列表
 	 *根据帖子的话题id查找帖子列表
 	 *@param  topicNum�? 	 *@return 返回list型；
 	 */
   public ArrayList<String> searchByTopicPostList(int topicNum,int schoolNum){
	   ArrayList<String> list=new ArrayList<String>();
		Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
		ResultSet rs=null;//结果�?		
		PreparedStatement pstmt=null;
		
		String sql="select post.post_num from post,user1 where post.topic_num='"+topicNum+"' and user1.account=post.account and user1.school_num='"+schoolNum+"'";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				list.add(rs.getString(1));
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return list;
   }
   
   /**
	 *统计帖子的评论数
	 *根据帖子的话题id统计帖子的评论数
	 *@param  post_num;
	 *@return 返回int;
	 */
   public int replyCountOfPost(int postNum){
	   int reply=0;
	   	Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
		ResultSet rs=null;//结果�?		
		PreparedStatement pstmt=null;
		
		String sql="select count(*) from direct_post where post_num='"+postNum+"'";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				reply=rs.getInt(1);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
	   
	   return reply;
   }
   
   /**
	 *找到帖子的最后评论人
	 *根据帖子的话题id找到帖子的最后评论人
	 *@param  post_num;
	 *@return 返回String user1.nickname;
	 */
  public String theLastPeople(int postNum){
	  String account = "nihao";
		Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
		ResultSet rs=null;//结果�?		
		PreparedStatement pstmt=null;
		
		String sql="select user1.account from direct_post,user1 where direct_post.account=user1.account and direct_post.post_num='"+postNum+"' order by time DESC;";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				account=rs.getString(1);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return account;
  }
  
  /**
   * 根据帖子id找到发帖�?   * @param postNum
   * @retrun String account;
   */
  public String getAccountOfAuthor(int postNum){
	  String author = null;
	  Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
	  ResultSet rs=null;//结果�?		
	  PreparedStatement pstmt=null;
		
		String sql="select account from post where post_num='"+postNum+"'";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				author=rs.getString(1);
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return author;
  }
  
  /**
	 *得到�?��列主帖简要信息方�?	 *根据�?��列用户查找并显示这些帖子的简要信�?	 *@param  ArrayList�?	 *@return 返回ArrayList<Post>型；
	 */
  public ArrayList<Post> postListbyAccountList(ArrayList<String> accountList){
	  ArrayList<Post> postList=new ArrayList<Post>();
	   Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
	   ResultSet rs=null;//结果�?		
	   PreparedStatement pstmt=null;
		try{
		for(int i=0;i<accountList.size();i++){
		String sql="select * from post where account='"+accountList.get(i)+"'";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				Post e=new Post();
				e.setPost_num(rs.getInt("post_num"));
				e.setAccount(rs.getString("account"));
				e.setContent(rs.getString("content"));
				e.setImageUrl(rs.getString("imageurl"));
				e.setTheme(rs.getString("theme"));
				e.setTopic_num(rs.getInt("topic_num"));
				e.setTime(rs.getString("time"));
				postList.add(e);
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return postList;   
  }
  
  
  /**
	 *得到�?��列主帖简要信息方�?	 *根据�?��列用户查找并显示这些帖子的简要信�?	 *@param  ArrayList�?	 *@return 返回ArrayList<Post>型；
	 */
  	public boolean insertIntoPost(String content, String theme, String account,
		int topicNum, String time, String imageUrl) {
  		boolean bool=false;
  		Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
  		int crow;//结果�?		
  		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		String sql="insert into post(content,theme,account,topic_num,time,imageurl) values('"+content+"','"+theme+"','"+account+"','"+topicNum+"','"+time+"','"+imageUrl+"')";
		try{
			pstmt=conn.prepareStatement(sql);
		    crow=pstmt.executeUpdate(sql);
		    if(crow==0) bool=false;
		    else if(crow==1) bool=true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return bool;

	}

}

